package com.ecar.apm.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecar.apm.dao.HttpSequenceLogMapper;
import com.ecar.apm.dao.HttpSequenceMapper;
import com.ecar.apm.model.HttpSequence;
import com.ecar.apm.model.HttpSequenceLog;
import com.ecar.apm.model.HttpSystem;
import com.ecar.apm.model.MonitorFrequency;
import com.ecar.apm.model.RecentStatus;
import com.ecar.apm.service.HttpSequenceService;
import com.ecar.apm.util.GuidGenerator;
import com.ecar.apm.util.MathUtil;
import com.github.pagehelper.StringUtil;

@Service
public class HttpSequenceServiceImpl implements HttpSequenceService{

	@Autowired
	  private HttpSequenceMapper httpSequenceMapper;
	  @Autowired
	  private HttpSequenceLogMapper httpSequenceLogMapper;
	  
	  public HttpSequence getByGuid(String guid)
	  {
	    return this.httpSequenceMapper.getByGuid(guid);
	  }
	  
	  public void archived(String guid)
	  {
	    this.httpSequenceMapper.archived(guid);
	  }
	  
	  public void updateEnabled(HttpSequence httpSequence)
	  {
	    this.httpSequenceMapper.updateEnabled(httpSequence);
	  }
	  
	  public void insert(HttpSequence httpSequence)
	  {
	    if (StringUtil.isEmpty(httpSequence.getGuid())) {
	      httpSequence.setGuid(GuidGenerator.generate());
	    }
	    this.httpSequenceMapper.insert(httpSequence);
	  }
	  
	  public void update(HttpSequence httpSequence)
	  {
	    this.httpSequenceMapper.update(httpSequence);
	  }
	  
	  public List<Map<String, Object>> getMonitorList()
	  {
	    List<RecentStatus> recentList = this.httpSequenceMapper.selectMonitorDetailList();
	    
	    List<Map<String, Object>> list = new ArrayList();
	    if (recentList != null) {
	      for (RecentStatus myList : recentList)
	      {
	        Map<String, Object> item = new HashMap();
	        String newStr = myList.getUrl().replace("http://", "");
	        int lenStr = newStr.indexOf("/");
	        String urlStr = newStr;
	        if (lenStr > 0) {
	          urlStr = newStr.substring(0, newStr.indexOf("/"));
	        }
	        item.put("urlStr", urlStr);
	        item.put("frequency", MonitorFrequency.valueOf(myList.getFrequency()).getLabel());
	        item.put("type", HttpSequence.getMonitorTypeName(String.valueOf(myList.getType())));
	        item.put("group", myList.getGroup());
	        item.put("guid", myList.getGuid());
	        item.put("name", myList.getName());
	        item.put("enabled", myList.getEnabled());
	        if (((Boolean)myList.getEnabled()).booleanValue())
	        {
	          item.put("avgCostTime", Integer.valueOf(myList.getAvgCostTime()));
	          switch (myList.getStatus())
	          {
	          case 1: 
	            item.put("status", "����");
	            item.put("textColor", "text-green");
	            break;
	          case 0: 
	            item.put("status", "����");
	            item.put("textColor", "text-red");
	            break;
	          default: 
	            item.put("status", "������");
	            item.put("textColor", "text-muted");
	          }
	          item.put("lastCost", Integer.valueOf(myList.getLastCost()));
	          item.put("moniCount", Integer.valueOf(myList.getMoniCount()));
	          item.put("minCost", Integer.valueOf(myList.getMinCost()));
	          item.put("maxCost", Integer.valueOf(myList.getMaxCost()));
	          if (myList.getCreateTime() != null) {
	            item.put("createTime", myList.getCreateTime().replace("-", ":"));
	          } else {
	            item.put("createTime", myList.getCreateTime());
	          }
	          item.put("fCount", Integer.valueOf(myList.getErrorCount()));
	          int aCount = myList.getMoniCount();
	          int eCount = myList.getErrorCount();
	          item.put("usability", MathUtil.percent(aCount - eCount, aCount));
	        }
	        else
	        {
	          item.put("status", "������");
	          item.put("textColor", "text-muted");
	          item.put("usability", "0%");
	          item.put("avgCostTime", "0");
	          item.put("moniCount", Integer.valueOf(0));
	          item.put("minCost", Integer.valueOf(0));
	          item.put("maxCost", Integer.valueOf(0));
	          item.put("fCount", Integer.valueOf(0));
	          item.put("lastCost", Integer.valueOf(0));
	          item.put("createTime", "00:00:00");
	        }
	        list.add(item);
	      }
	    }
	    return list;
	  }
	  
	  public List<Map<String, Object>> getLogByGuidForEcharts(String guid)
	  {
	    List<Map<String, Object>> list = this.httpSequenceLogMapper.selectLogByPguidForEcharts(guid);
	    return list;
	  }
	  
	  public List<Map<String, Object>> getLogByGuidForEchartsPie(String guid)
	  {
	    List<Map<String, Object>> list = this.httpSequenceLogMapper.selectLogByPguidForEchartsPie(guid);
	    return list;
	  }
	  
	  public List<Map<String, Object>> getLogByGuid(String guid)
	  {
	    List<Map<String, Object>> list = this.httpSequenceLogMapper.selectLogByPguid(guid);
	    return list;
	  }
	  
	  public boolean addHttpSystem(String group)
	  {
	    HttpSystem httpSystem = new HttpSystem();
	    httpSystem.setName(group);
	    try
	    {
	      this.httpSequenceMapper.insertSystem(httpSystem);
	      return true;
	    }
	    catch (Exception e) {}
	    return false;
	  }
	  
	  public List<HttpSystem> getAllSystem()
	  {
	    return this.httpSequenceMapper.selectAllSystem();
	  }
	  
	  public void insertLog(HttpSequenceLog httpSequenceLog)
	  {
	    this.httpSequenceLogMapper.insert(httpSequenceLog);
	  }
	  
	  public void deleteLog(String pguid)
	  {
	    this.httpSequenceLogMapper.delete(pguid);
	  }
	  
	  public void cleanLog(int day)
	  {
	    this.httpSequenceLogMapper.cleanLogByDay(day);
	  }
}
